package com.hzhc.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.CntrLog;
import com.hzhc.mapper.CntrLogMapper;
import com.hzhc.service.CntrLogService;
import com.hzhc.vo.CntrLogVo;
import com.hzhc.vo.DataAnalyVo;
import com.hzhc.vo.ProductionVo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * <p>
 * 集装箱日志表 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@Service
public class CntrLogServiceImpl extends ServiceImpl<CntrLogMapper, CntrLog> implements CntrLogService {

    @Override
    public  List<ProductionVo> getProductionList(ProductionVo vo) {

        List<ProductionVo> resultList = new ArrayList<>();
        List<CntrLogVo> cntrLogVos = baseMapper.productionList(vo.getYear(),vo.getCompanyId());
        //收箱
        Integer lcxTue = 0;
        Integer lcdTue = 0;
        BigDecimal lcWeight = BigDecimal.valueOf(0.0);
        BigDecimal lcGoodsValue = BigDecimal.valueOf(0.0);
        //提箱
        Integer txxTue = 0;
        Integer txdTue = 0;
        BigDecimal txWeight = BigDecimal.valueOf(0.0);
        BigDecimal txGoodsValue = BigDecimal.valueOf(0.0);
        //空箱
        Integer exTue = 0;
        Integer edTue = 0;
        BigDecimal eWeight = BigDecimal.valueOf(0.0);
        BigDecimal eGoodsValue = BigDecimal.valueOf(0.0);
        //重箱
        Integer zxTue = 0;
        Integer zdTue = 0;
        BigDecimal zWeight = BigDecimal.valueOf(0.0);
        BigDecimal zGoodsValue = BigDecimal.valueOf(0.0);
        for(CntrLogVo clv:cntrLogVos){
            String size = clv.getCntrSize().substring(0, 2);
            if(clv.getRemark().equals("卸车理货(入场)") || clv.getRemark().contains("收箱理货")){
                if(Integer.parseInt(size)<40){
                    lcxTue++;
                }else{
                    lcdTue++;
                }
                if(!StringUtils.isEmpty(clv.getWeight())){
                    BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                    lcWeight = lcWeight.add(w);
                }
                if(!StringUtils.isEmpty(clv.getGoodsValue())){
                    BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                    lcGoodsValue = lcGoodsValue.add(g);
                }
            }else if(clv.getRemark().equals("装车理货(出场)") || clv.getRemark().equals("提箱理货")){
                if(Integer.parseInt(size)<40){
                    txxTue++;
                }else{
                    txdTue++;
                }
                if(!StringUtils.isEmpty(clv.getWeight())){
                    BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                    txWeight = txWeight.add(w);
                }
                if(!StringUtils.isEmpty(clv.getGoodsValue())){
                    BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                    txGoodsValue = txGoodsValue.add(g);
                }
            }
//            else if(clv.getRemark().contains("查验")){
//                if(Integer.parseInt(size)<40){
//                    cyxTue++;
//                }else{
//                    cydTue++;
//                }
//                if(!StringUtils.isEmpty(clv.getWeight())){
//                    BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
//                    cyWeight = cyWeight.add(w);
//                }
//                if(!StringUtils.isEmpty(clv.getGoodsValue())){
//                    BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
//                    cyGoodsValue = cyGoodsValue.add(g);
//                }
//            }
            if (clv.getEfFlag() !=null && "".equals(clv.getEfFlag())){
                if(clv.getEfFlag().equals("E")){
                    if(Integer.parseInt(size)<40){
                        exTue++;
                    }else{
                        edTue++;
                    }
                    if(!StringUtils.isEmpty(clv.getWeight())){
                        BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                        eWeight = eWeight.add(w);
                    }
                    if(!StringUtils.isEmpty(clv.getGoodsValue())){
                        BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                        eGoodsValue = eGoodsValue.add(g);
                    }
                }else{
                    if(Integer.parseInt(size)<40){
                        zxTue++;
                    }else{
                        zdTue++;
                    }
                    if(!StringUtils.isEmpty(clv.getWeight())){
                        BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                        zWeight = zWeight.add(w);
                    }
                    if(!StringUtils.isEmpty(clv.getGoodsValue())){
                        BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                        zGoodsValue = zGoodsValue.add(g);
                    }
                }
            }


        }

        ProductionVo vo1 = new ProductionVo();
        ProductionVo vo2 = new ProductionVo();
        ProductionVo vo3 = new ProductionVo();
        ProductionVo vo4 = new ProductionVo();

        vo1.setName("20GP");
        vo1.setSx(lcxTue);
        vo1.setTx(txxTue);
//        vo1.setCy(cyxTue);
        vo1.setKx(exTue);
        vo1.setZx(zxTue);

        vo2.setName("40GP");
        vo2.setSx(lcdTue);
        vo2.setTx(txdTue);
//        vo2.setCy(cydTue);
        vo2.setKx(edTue);
        vo2.setZx(zdTue);

        vo3.setName("货重");
        vo3.setSx(lcWeight);
        vo3.setTx(txWeight);
//        vo3.setCy(cyWeight);
        vo3.setKx(eWeight);
        vo3.setZx(zWeight);

        vo4.setName("货值");
        vo4.setSx(lcGoodsValue);
        vo4.setTx(txGoodsValue);
//        vo4.setCy(cyGoodsValue);
        vo4.setKx(eGoodsValue);
        vo4.setZx(zGoodsValue);

        resultList.add(vo1);
        resultList.add(vo2);
        resultList.add(vo3);
        resultList.add(vo4);


        return resultList;
    }

    @Override
    public List<CntrLogVo> getSiteTallyList(Page<CntrLogVo> page, CntrLogVo vo) {
        List<CntrLogVo> cntrLogVos = baseMapper.selectSiteTallyList(page, vo);
        return cntrLogVos;
    }

    @Override
    public Map<String, Object> getSiteTallyLine(CntrLogVo vo) {
        String year = vo.getYear();
        Map<String, Object> map = new HashMap<>();
        Map<String, Map<String, Long>> xcData = baseMapper.selectXcData(vo,year);//卸车理货
        Map<String, Map<String, Long>> zcData = baseMapper.selectZcData(vo,year);//装车理货
        Map<String, Map<String, Long>> sxData = baseMapper.selectSxData(vo,year);//收箱理货
        Map<String, Map<String, Long>> txData = baseMapper.selectTxData(vo,year);//提箱理货
        List<String> month = new ArrayList<>();
        List<Long> xc = new ArrayList<>();
        List<Long> zc = new ArrayList<>();
        List<Long> tx = new ArrayList<>();
        List<Long> sx = new ArrayList<>();
        String m;
        for (int i = 1; i <= 12; i++) {
            m = i > 9 ? i + "月" : "0" + i + "月";
            month.add(m);
            xc.add(xcData.get(m) == null ? 0L : xcData.get(m).get("cntrNum"));
            zc.add(zcData.get(m) == null ? 0L : zcData.get(m).get("cntrNum"));
            sx.add(sxData.get(m) == null ? 0L : sxData.get(m).get("cntrNum"));
            tx.add(zcData.get(m) == null ? 0L : txData.get(m).get("cntrNum"));
        }
        map.put("month", month);
        map.put("xc", xc);
        map.put("zc", zc);
        map.put("sx", sx);
        map.put("tx", tx);
        return map;

    }

    @Override
    public Map<String, Object> getProductionList1(ProductionVo vo) {

        List<CntrLogVo> cntrLogVos = baseMapper.productionList(vo.getYear(),vo.getCompanyId());
        JSONObject obj = new JSONObject();
        //落场
        Integer lcxTue = 0;
        Integer lcdTue = 0;
        BigDecimal lcWeight = BigDecimal.valueOf(0.0);
        BigDecimal lcGoodsValue = BigDecimal.valueOf(0.0);
        //提箱
        Integer txxTue = 0;
        Integer txdTue = 0;
        BigDecimal txWeight = BigDecimal.valueOf(0.0);
        BigDecimal txGoodsValue = BigDecimal.valueOf(0.0);
//        //查验
//        Integer cyxTue = 0;
//        Integer cydTue = 0;
//        BigDecimal cyWeight = BigDecimal.valueOf(0.0);
//        BigDecimal cyGoodsValue = BigDecimal.valueOf(0.0);
        //空箱
        Integer exTue = 0;
        Integer edTue = 0;
        BigDecimal eWeight = BigDecimal.valueOf(0.0);
        BigDecimal eGoodsValue = BigDecimal.valueOf(0.0);
        //重箱
        Integer zxTue = 0;
        Integer zdTue = 0;
        BigDecimal zWeight = BigDecimal.valueOf(0.0);
        BigDecimal zGoodsValue = BigDecimal.valueOf(0.0);
        for(CntrLogVo clv:cntrLogVos){
            String size = clv.getCntrSize().substring(0, 2);
            if(clv.getRemark().equals("入场理货") || clv.getRemark().equals("落箱理货")){
                if(Integer.parseInt(size)<40){
                    lcxTue++;
                }else{
                    lcdTue++;
                }
                if(!StringUtils.isEmpty(clv.getWeight())){
                    BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                    lcWeight = lcWeight.add(w);
                }
                if(!StringUtils.isEmpty(clv.getGoodsValue())){
                    BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                    lcGoodsValue = lcGoodsValue.add(g);
                }
            }else if(clv.getRemark().equals("出场理货")){
                if(Integer.parseInt(size)<40){
                    txxTue++;
                }else{
                    txdTue++;
                }
                if(!StringUtils.isEmpty(clv.getWeight())){
                    BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                    txWeight = txWeight.add(w);
                }
                if(!StringUtils.isEmpty(clv.getGoodsValue())){
                    BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                    txGoodsValue = txGoodsValue.add(g);
                }
            }

            if (clv.getEfFlag() !=null && "".equals(clv.getEfFlag())){
                if(clv.getEfFlag().equals("E")){
                    if(Integer.parseInt(size)<40){
                        exTue++;
                    }else{
                        edTue++;
                    }
                    if(!StringUtils.isEmpty(clv.getWeight())){
                        BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                        eWeight = eWeight.add(w);
                    }
                    if(!StringUtils.isEmpty(clv.getGoodsValue())){
                        BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                        eGoodsValue = eGoodsValue.add(g);
                    }
                }else{
                    if(Integer.parseInt(size)<40){
                        zxTue++;
                    }else{
                        zdTue++;
                    }
                    if(!StringUtils.isEmpty(clv.getWeight())){
                        BigDecimal w = BigDecimal.valueOf(Long.parseLong(clv.getWeight()));
                        zWeight = zWeight.add(w);
                    }
                    if(!StringUtils.isEmpty(clv.getGoodsValue())){
                        BigDecimal g = BigDecimal.valueOf(Long.parseLong(clv.getGoodsValue()));
                        zGoodsValue = zGoodsValue.add(g);
                    }
                }
            }


        }
        obj.put("lcxTue",lcxTue);
        obj.put("lcdTue",lcdTue);
        obj.put("lcWeight",lcWeight);
        obj.put("lcGoodsValue",lcGoodsValue);

        obj.put("txxTue",txxTue);
        obj.put("txdTue",txdTue);
        obj.put("txWeight",txWeight);
        obj.put("txGoodsValue",txGoodsValue);

//        obj.put("cyxTue",cyxTue);
//        obj.put("cydTue",cydTue);
//        obj.put("cyWeight",cyWeight);
//        obj.put("cyGoodsValue",cyGoodsValue);

        obj.put("exTue",exTue);
        obj.put("edTue",edTue);
        obj.put("eWeight",eWeight);
        obj.put("eGoodsValue",eGoodsValue);

        obj.put("zxTue",zxTue);
        obj.put("zdTue",zdTue);
        obj.put("zWeight",zWeight);
        obj.put("zGoodsValue",zGoodsValue);
        return obj;
    }

    @Override
    public List<CntrLogVo> getList(CntrLogVo cntrLog, Page<CntrLogVo> pagination) {
        IPage<CntrLogVo> pageList = baseMapper.selectCntrLogPage(pagination, cntrLog);
        return pageList.getRecords();
    }

    @Override
    public List<CntrLogVo> getYzsCntrLogsList(CntrLogVo cntrLog, Page<CntrLogVo> pagination) {
        IPage<CntrLogVo> pageList = baseMapper.selectYzsCntrLogPage(pagination, cntrLog);
        return pageList.getRecords();
    }

    @Override
    public Map<String, Object> dataAnaly() {
        List<String> months = Arrays.asList("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
        Map<String, Object> result = new LinkedHashMap<>();
        //本年
        LocalDate start = LocalDate.now().with(TemporalAdjusters.firstDayOfYear());
        LocalDate end = LocalDate.now().with(TemporalAdjusters.lastDayOfYear());
        Map<String, Map<String, BigDecimal>> month = baseMapper.selectTotal(start, end);
        Map<String, BigDecimal> nowSortData = mapKeySort(month);
        List<String> m0 = new ArrayList<>();
        List<String> nowTon = new ArrayList<>();
        for (String m : months) {
            if (LocalDate.now().getMonthValue() < Integer.parseInt(m)) {
                break;
            }
            String t = "0";
            for (String s : nowSortData.keySet()) {
                if (s.substring(4).equals(m)) {
                    t = nowSortData.get(s).stripTrailingZeros().toPlainString();
                    break;
                }
            }
            m0.add(m);
            nowTon.add(t);
        }
        result.put("month", m0);
        Map<String, Object> nowMonth = new LinkedHashMap<>();
        nowMonth.put("ton", nowTon);
        result.put("now", nowMonth);
        //同比
        Map<String, Map<String, BigDecimal>> tMonths = baseMapper.selectTotal(LocalDate.now().minusYears(1).with(TemporalAdjusters.firstDayOfYear()),
                LocalDate.now().minusYears(1).with(TemporalAdjusters.lastDayOfYear()));
        Map<String, BigDecimal> tSortData = mapKeySort(tMonths);
        List<String> tTon = new ArrayList<>();
        for (String m : months) {
            if (LocalDate.now().getMonthValue() < Integer.parseInt(m)) {
                break;
            }
            String t = "0";
            for (String s : nowSortData.keySet()) {
                if (s.substring(4).equals(m)) {
                    t = nowSortData.get(s).stripTrailingZeros().toPlainString();
                    break;
                }
            }
            tTon.add(t);
        }
        Map<String, Object> tbMonth = new LinkedHashMap<>();
        tbMonth.put("ton", nowTon);
        tbMonth.put("tongTon", tTon);
        result.put("tong", tbMonth);
        //环比
        Map<String, Map<String, BigDecimal>> hMonths = baseMapper.selectTotal(start.minusMonths(1), end.minusMonths(1));
        Map<String, BigDecimal> hSortData = mapKeySort(hMonths);
        List<String> hTon = new ArrayList<>();
        List<String> monthLast = Arrays.asList("12", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11");
        for (String m : monthLast) {
            if (!"12".equals(m) && LocalDate.now().getMonthValue() <= Integer.parseInt(m)) {
                break;
            }
            String t = "0";
            for (String s : nowSortData.keySet()) {
                if (s.substring(4).equals(m)) {
                    t = nowSortData.get(s).stripTrailingZeros().toPlainString();
                    break;
                }
            }
            hTon.add(t);
        }
        Map<String, Object> hbMonth = new LinkedHashMap<>();
        hbMonth.put("ton", nowTon);
        hbMonth.put("huanTon", hTon);
        result.put("huan", hbMonth);

        return result;
    }

    @Override
    public Map<String, Object> dataTeuAnaly(DataAnalyVo vo) {
        List<DataAnalyVo> data = baseMapper.selectTeuTotal(vo);
        Map<String, Object> returnMap=new HashMap<>();
        returnMap.put("ZC",0);
        returnMap.put("XC",0);
        returnMap.put("SX",0);
        returnMap.put("TX",0);
        for (DataAnalyVo datum : data) {
            if(datum.getRemark().indexOf("装车")!=-1){
                returnMap.put("ZC",Integer.valueOf(returnMap.get("ZC").toString())+Integer.valueOf(datum.getNum()));
            }
            if(datum.getRemark().indexOf("卸车")!=-1){
                returnMap.put("XC",Integer.valueOf(returnMap.get("XC").toString())+Integer.valueOf(datum.getNum()));
            }
            if(datum.getRemark().indexOf("收箱")!=-1){
                returnMap.put("SX",Integer.valueOf(returnMap.get("SX").toString())+Integer.valueOf(datum.getNum()));
            }
            if(datum.getRemark().indexOf("提箱")!=-1){
                returnMap.put("TX",Integer.valueOf(returnMap.get("TX").toString())+Integer.valueOf(datum.getNum()));
            }
        }
       return returnMap;
    }

    private Map<String, BigDecimal> mapKeySort(Map<String, Map<String, BigDecimal>> map) {
        Map<String, BigDecimal> sort = new LinkedHashMap<>();
        for (String s : map.keySet()) {
            sort.put(s, map.get(s).get("num"));
        }
        List<Map.Entry<String, BigDecimal>> list = new ArrayList<>(sort.entrySet());
        list.sort(Map.Entry.comparingByKey());
        Map<String, BigDecimal> sortData = new LinkedHashMap<>();
        for (Map.Entry<String, BigDecimal> entry : list) {
            sortData.put(entry.getKey(), entry.getValue());
        }
        return sortData;
    }

    @Override
    public JSONArray getTodayTPS() {
        List<CntrLogVo> cntrLogVos = baseMapper.getTodayTPS();//出场+入场+收箱+提箱
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
//        Integer zcCntrCount = 0;//(装车)出场
//        Integer xcCntrCount = 0;//(卸车)入场
//        Integer tCntrCount = 0;//提箱
//        Integer lCntrCount = 0;//收箱
        long xtx = cntrLogVos.stream().filter(u -> u.getRemark().contains("提箱") && u.getCntrSize().startsWith("20")).count();//提箱 20
        long dtx = cntrLogVos.stream().filter(u -> u.getRemark().contains("提箱") && u.getCntrSize().startsWith("40")).count();//提箱 40
        long xsx = 0;//收箱 20
        long dsx = 0;//收箱 40
        long zcx = cntrLogVos.stream().filter(u -> u.getRemark().contains("出场") && u.getCntrSize().startsWith("20")).count();//(装车)出场 20
        long zcd = cntrLogVos.stream().filter(u -> u.getRemark().contains("出场") && u.getCntrSize().startsWith("40")).count();//(装车)出场 40
        long xcx = cntrLogVos.stream().filter(u -> u.getRemark().contains("入场") && u.getCntrSize().startsWith("20")).count();//(卸车)入场 20
        long xcd = cntrLogVos.stream().filter(u -> u.getRemark().contains("入场") && u.getCntrSize().startsWith("40")).count();//(卸车)入场 40

        List<CntrLogVo> rel = new ArrayList<>();
        CntrLogVo vo = new CntrLogVo();
        vo.setValue(zcx+zcd*2).setName("出场");
        vo.setValue(xcx+xcd*2).setName("入场");
        vo.setValue(xtx+dtx*2).setName("出场");
        vo.setValue(xsx+dsx*2).setName("出场");
        rel.add(vo);
        obj.put("value",zcx+zcd*2);
        obj.put("name","装车");
        array.add(obj);
        JSONObject obj1 = new JSONObject();
        obj1.put("value",xcx+xcd*2);
        obj1.put("name","卸车");
        array.add(obj1);
        JSONObject obj2 = new JSONObject();
        obj2.put("value",xtx+dtx*2);
        obj2.put("name","提箱");
        array.add(obj2);
        JSONObject obj3 = new JSONObject();
        obj3.put("value",xsx+dsx*2);
        obj3.put("name","收箱");
        array.add(obj3);
        return array;
    }

    @Override
    public List<CntrLogVo> getAppLogList(Page<CntrLogVo> page, CntrLogVo vo) {
        IPage<CntrLogVo> pageList = baseMapper.selectCntrLogPage(page, vo);
        return pageList.getRecords();
    }
}
